<template>
  <el-card class="box-card">
    <div class="model-role">
      <el-form :inline="true" :model="dataForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="dataForm.equipmentName" clearable placeholder="设备名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.equipmentType" clearable placeholder="设备类型"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDateList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openDialog()">添加设备</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border style="width: 100%">
        <el-table-column prop="equipmentName" label="设备名称" width="200"></el-table-column>
        <el-table-column prop="equipmentType" label="设备类型" width="200"></el-table-column>
        <el-table-column prop="equipmentModel" label="设备型号" width="200"></el-table-column>
        <el-table-column prop="equipmentManufacturer" label="供应厂商" width="200"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        style="margin-top: 30px">
      </el-pagination>
    </div>

    <el-dialog :title="dataDialogForm.equipmentName === '' ?  dialogTitle:dialogTitle='更新设备信息' "
               :visible.sync="dialogFormVisible" @close="closeDialog">
      <el-form :model="dataDialogForm" :rules="rules" ref="ruleForm">
        <el-form-item label="设备名称" :label-width="formLabelWidth">
          <el-input v-model="dataDialogForm.equipmentName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备类型" :label-width="formLabelWidth">
          <el-input v-model="dataDialogForm.equipmentType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备型号" :label-width="formLabelWidth" prop="role_name">
          <el-input v-model="dataDialogForm.equipmentModel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="供应厂商" :label-width="formLabelWidth" prop="role_name">
          <el-input v-model="dataDialogForm.equipmentManufacturer" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm()">确 定</el-button>
      </div>
    </el-dialog>

  </el-card>
</template>

<script>

export default {
  name: 'customer',
  data() {
    return {
      dialogTitle: '添加设备',
      dataForm: {
        equipmentName: "",
        equipmentType: "",
      },
      ruleForm: {
        pass: '',
      },
      dataDialogForm: {
        id: '',
        equipmentName: '',
        equipmentType: "",
        equipmentModel: "",
        equipmentManufacturer: "",

      },
      dataList: [],
      userName: "xiaohei",
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      datalistLoading: false,
      dialogFormVisible: false,
      dialogFormSubmitVisible: false,
      formLabelWidth: '100px',
      rules: {

      }
    };
  },
  methods: {
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDateList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDateList()
    },
    getDateList() {
      // this.datalistLoading = true
      //生命params 分页查询的参数
      const params = {params: {pageIndex: this.pageIndex, pageSize: this.pageSize, equipmentName: this.dataForm.equipmentName, equipmentType: this.dataForm.equipmentType }}
      this.$http.get('/material/findByPage', params).then((res) => {
        console.log(res)
        this.dataList = res.data.data.records
        this.totalPage = res.data.data.total
      })
    },
    handleEdit(index, item) {
      //打开更新窗口

      this.dataDialogForm.id = item.id
      this.dataDialogForm.equipmentName = item.equipmentName
      this.dataDialogForm.equipmentType = item.equipmentType
      this.dataDialogForm.equipmentModel = item.equipmentModel
      this.dataDialogForm.equipmentManufacturer = item.equipmentManufacturer
      console.log('item', item)
      this.openDialog();
    },
    handleDelete(index, item) {
      this.$confirm('此操作将永久删除该区域信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/material/' + item.id).then((res) => {
          if (res.data.code == 200) {
            this.getDateList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            });
          }
          
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加用户按钮绑定的方法
    openDialog() {
      if (!this.dataDialogForm.id) {
        this.dialogTitle = '添加设备';
      } else {
        this.dialogTitle = '更新设备';
      }
      //打开窗口
      this.dialogFormVisible = true
    },
    closeDialog() {
      this.dataDialogForm.id = null;
      this.dialogFormVisible = false;
      //重置
      this.$refs.ruleForm.resetFields();
      //所有字段置空
      this.dataDialogForm = {
        id: '',
        equipmentName: '',
        equipmentType: "",
        equipmentModel: "",
        equipmentManufacturer: "",
        usageArea: "",
      }
    },
    handleConfirm() {
      // 当点击确定按钮时，访问当前对话框的标题
      const title = this.dialogTitle;
      console.log('当前对话框标题:', title);

      // 根据标题执行不同的逻辑
      if (!this.dataDialogForm.id) {
        this.handleSubmitFormData('ruleForm');
      } else if (title === '更新设备信息') {
        this.handleUpdateFormData('ruleForm');
      }

      // 可以在这里添加其他需要的逻辑
    },
    handleSubmitFormData(ruleForm) {
      //打开窗口
      // this.dialogFormVisible = true
      this.addMaterial(ruleForm)
    },
    handleUpdateFormData(ruleForm) {
      this.updateMaterial(ruleForm)
    },
    updateMaterial(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        this.$http.put('/material', this.dataDialogForm).then((res) => {
          console.log(res)
          this.dialogFormVisible = false   //关闭窗口
          this.closeDialog()
          this.getDateList()
        })
      });
    },
    addMaterial(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          if (this.dialogFormSubmitVisible) {
            return
          }
        } else {
          return false;
        }
      });
      this.dialogFormSubmitVisible = true;
      this.$http.post('/material', this.dataDialogForm).then((res) => {
        console.log(res)
        this.dialogFormVisible = false   //关闭窗口
        this.closeDialog()
        this.getDateList()
      })

    }
  },
  mounted() {
    this.getDateList()
  },
}

</script>

